<?php

/**
 * 护理评估模块相关接口
 * @authors Hex (hex@zpdchina.com)
 * @date    2017-03-06 15:26:18
 * @version $Id$
 */
class HulipingguController extends Yaf_Controller_Abstract
{
    /**
     * 护理评估保存
     * @return Response
     */
    public function hulipingguSaveAction()
    {
        //获取参数
        $request = $this->getRequest();
        $yiyuan_id = $request->getPost('yiyuan_id', 0);
        $data = $request->getPost('data', '');
        $upload_data = json_decode($data,true);
//        if (empty($yiyuan_id))
//            Utility::apiJsonError(30001, '医院编号不能为空');
        if (empty($upload_data))
            Utility::apiJsonError(30002, '上传数据不能为空');
        $hlpg_mdl = new HuliPingguModel();
        if (!empty($upload_data) && is_array($upload_data)) {
            //取最新的一条评估护理单
            $isCheck = $hlpg_mdl->getHulidanInfo($upload_data[0]['biaodan_id'], $upload_data[0]['zhuyuan_id'], $yiyuan_id);
            if (!empty($isCheck)) {
                $nowtime = date("Y-m-d H:i:s");
                //风险单id存在、护理周期为0或者开启护理措施功能 且护理开始时间<=当前时间<=结束时间 且最后一天的护理措施未执行完成
                if (($isCheck['huli_zhouqi'] > 0) && ($isCheck['start_time'] <= $nowtime) && ($nowtime <= $isCheck['finish_time'])) {
                    //获取护理周期最后一天未完成的护理措施
                    $zhixing_data = $hlpg_mdl->getUnzhixingData($isCheck['id'], $isCheck['finish_time'], $yiyuan_id);
                }
                if ((!empty($isCheck['pingfen_id']) && $isCheck['huli_zhouqi'] == 0)) {
//                    Utility::apiJsonError(30003, '该患者无需再评估');
                    $result['staus'] = '2';
                    $result['result'] = "该患者无需再评估";
                } elseif (Yaconf::get("config$yiyuan_id.is_hulipinggu_cuoshi") && !empty($zhixing_data)) {
//                    Utility::apiJsonError(30004, '在护理周期内，无需再评估');
                    $result['staus'] = '4';
                    $result['result'] = "在护理周期内，无需再评估";
                } else {
                    $result = $hlpg_mdl->saveData($upload_data, $yiyuan_id);
                }
            } else {
                $result = $hlpg_mdl->saveData($upload_data, $yiyuan_id);
            }
            $result['error'] = 200;
            $result['msg'] = "";
            Utility::apiJsonResultExtends($result);
        }
    }

    //接收上传过来的措施执行History数据
    public function cuoshiZhixingDataUploadAction()
    {
        //获取参数
        $request = $this->getRequest();
        $yiyuan_id = $request->getPost('yiyuan_id', 0);
        $data = $request->getPost('data', '');
        $upload_data = json_decode($data,true);
//        if (empty($yiyuan_id))
//            Utility::apiJsonError(30001, '医院编号不能为空');
        if (empty($upload_data))
            Utility::apiJsonError(30002, '上传数据不能为空');
//        $hlpg_mdl = new HuliPingguModel();
//        $affect = $hlpg_mdl->updateData($upload_data,$yiyuan_id);
        $table = 'zhuyuan_huli_pinggu_cuoshi_zhixing';
        $setArr = array('zhixing_hushi_id' => $upload_data['zhixing_hushi_id'],
            'zhixing_hushi_name' => $upload_data['zhixing_hushi_name'],
            'zhixinged_time' => $upload_data['zhixing_time'],
            'is_zhixing' => 1);
        $colArr = ['id', 'yiyuan_id'];
        $valArr = [$upload_data['zhixing_id'], $yiyuan_id];
        $affect = CommonModel::updateRows($table, $setArr, $colArr, $valArr);
//        if ($affect) {
//            Utility::apiJsonResult('', '执行成功');
//        }else{
//            Utility::apiJsonError(30006, '执行失败');
//        }
        $statr = array(
            'state' => $affect ? '1' : '0',
        );
        $statr['error'] = 200;
        $statr['msg'] = "";
        Utility::apiJsonResultExtends($statr);
    }

    //获取护理周期内的护理评估记录
    public function getHulipingguDataJsonAction()
    {
        //获取参数
        $request = $this->getRequest();
        $yiyuan_id = $request->getPost('yiyuan_id', 0);
        $zhuyuan_id = $request->getPost('zhuyuan_id', '');
        $jilu_shijian = $request->getPost('jilu_shijian', '');
        $biaodan_id = $request->getPost('biaodan_id', '');
        $is_from = $request->getPost('is_from', 'pinggu'); //pinggu是护理评估 cuoshi是护理措施 护理措施时需要过滤护理周期为0的数据
//        if (empty($yiyuan_id))
//            Utility::apiJsonError(30001, '医院编号不能为空');
        if (empty($zhuyuan_id))
            Utility::apiJsonError(30006, '患者住院编号不能为空');
        $hlpg_mdl = new HuliPingguModel();
        $pinggu_data = $hlpg_mdl->getPingguData($zhuyuan_id, $yiyuan_id, $biaodan_id, $jilu_shijian);
        if ($is_from == 'cuoshi') {
            foreach ($pinggu_data as $key => $val) {
                //没有护理措施的数据或者护理周期为0的数据清除 只展示有护理措施的数据
                $cuoshi_data = $hlpg_mdl->getCuoshiZhixing($val['id']);
                if (empty($pinggu_data[$key]['pingfen_id']) || empty($pinggu_data[$key]['huli_zhouqi']) || empty($cuoshi_data))
                    unset($pinggu_data[$key]);
                else
                    $pinggu_data_arr[] = $pinggu_data[$key];
            }
        } else
            $pinggu_data_arr = $pinggu_data;
//        if(!empty($pinggu_data_arr))
//            Utility::apiJsonResult($pinggu_data_arr);
//        else
//            Utility::apiJsonError(30007, '暂无相关数据');
        if (!empty($pinggu_data_arr))
            $data['state'] = '1';
        else
            $data['state'] = '0';
        $data['msg'] = "";
        $data['error'] = 200;
        $data['result'] = $pinggu_data_arr;
        Utility::apiJsonResultExtends($data);
    }

    //获取护理评估选项详细
    public function getHulipingguxiangmuAction()
    {
        //获取参数
        $request = $this->getRequest();
        $yiyuan_id = $request->getPost('yiyuan_id', 0);
        $hulipinggu_id = $request->getPost('hulipinggu_id', '');
//        if (empty($yiyuan_id))
//            Utility::apiJsonError(30001, '医院编号不能为空');
        if (empty($hulipinggu_id))
            Utility::apiJsonError(30008, '评估单编号不能为空');
        $hlpg_mdl = new HuliPingguModel();
        $pinggu_data = $hlpg_mdl->getXiangmuData($hulipinggu_id);
        if (!empty($pinggu_data))
            $data['state'] = '1';
        else
            $data['state'] = '0';
        $data['msg'] = "";
        $data['error'] = 200;
        $data['result'] = $pinggu_data;
        Utility::apiJsonResultExtends($data);
    }

    //获取护理评估措施详细
    public function getHulipingguCuoshiAction()
    {
        //获取参数
        $request = $this->getRequest();
        $yiyuan_id = $request->getPost('yiyuan_id', 0);
        $hulipinggu_id = $request->getPost('hulipinggu_id', '');//措施是否已经执行 0否,1是
        $iszhixing = $request->getPost('iszhixing');
//        if (empty($yiyuan_id))
//            Utility::apiJsonError(30001, '医院编号不能为空');
        if (empty($hulipinggu_id))
            Utility::apiJsonError(30008, '评估单编号不能为空');
        $hlpg_mdl = new HuliPingguModel();
        $cuoshi_data = $hlpg_mdl->getCuoshiZhixing($hulipinggu_id, true, $iszhixing);
        if (!empty($cuoshi_data))
            $data['state'] = '1';
        else
            $data['state'] = '0';
        $data['msg'] = "";
        $data['error'] = 200;
        $data['result'] = $cuoshi_data;
        Utility::apiJsonResultExtends($data);
    }

    //获取护理评估、护理措施提示数据
    public function getHulipingguRemindNumAction()
    {
        //获取参数
        $request = $this->getRequest();
        $yiyuan_id = $request->getPost('yiyuan_id', 0);
        $zhuyuan_id = $request->getPost('zhuyuan_id', '');
//        if (empty($yiyuan_id))
//            Utility::apiJsonError(30001, '医院编号不能为空');
        if (empty($zhuyuan_id))
            Utility::apiJsonError(30006, '患者住院编号不能为空');
        $hlpg_mdl = new HuliPingguModel();
        $data['state'] = '1';
        $data['result'] = $hlpg_mdl->getTongjiNum($zhuyuan_id, $yiyuan_id);
        $data['msg'] = "";
        $data['error'] = 200;
        Utility::apiJsonResultExtends($data);
    }

    //获取该病区需要护理评估或护理措施的患者
    public function getHulipingguTongjiAction()
    {
        //获取参数
        $request = $this->getRequest();
        $yiyuan_id = $request->getPost('yiyuan_id', 0);
        $user_department = $request->getPost('user_department', '');
//        if (empty($yiyuan_id))
//            Utility::apiJsonError(30001, '医院编号不能为空');
        if (empty($user_department))
            Utility::apiJsonError(30010, '科室不能为空');
        //获取该病区住院中的患者
        $fields = 't.zhuangtai,t.patient_id,t.zhuyuan_id,a.xingming,t.bingchuang_hao,a.nianling,a.xingbie,t.hulijibie,t.ruyuan_riqi_time';
        $patient_lists = CommonModel::getZhuyuanIdList($fields, $user_department, $yiyuan_id);
        $hlpg_mdl = new HuliPingguModel();
        $new_arr = array();
        if ($patient_lists) {
            foreach ($patient_lists as $key => $val) {
                $res = $hlpg_mdl->getTongjiNum($val['zhuyuan_id'], $yiyuan_id);
                $val["bingchuang_hao"] = str_replace("床", "", $val["bingchuang_hao"]);
                if ($res['pinggu_count'] || $res['cuoshi_count']) {
                    $val['pinggu_count'] = $res['pinggu_count'];
                    $val['cuoshi_count'] = $res['cuoshi_count'];
                    $new_arr[] = $val;
                }
            }
        }
        $data['state'] = '1';
        $data['result'] = $new_arr;
        $data['msg'] = "";
        $data['error'] = 200;
        Utility::apiJsonResultExtends($data);
    }

    //删除护理评估单接口
    public function delHulipingguAction()
    {
        //获取参数
        $request = $this->getRequest();
        $yiyuan_id = $request->getPost('yiyuan_id', 0);
        $hulipinggu_id = $request->getPost('hulipinggu_id', '');
//        if (empty($yiyuan_id))
//            Utility::apiJsonError(30001, '医院编号不能为空');
        if (empty($hulipinggu_id))
            Utility::apiJsonError(30008, '评估单编号不能为空');
        $hlpg_mdl = new HuliPingguModel();
        $pinggu_data = $hlpg_mdl->getXiangmuData($hulipinggu_id);
        $zhixing_data = $hlpg_mdl->getCuoshiZhixing($hulipinggu_id, false);
        $colArr = ['id', 'yiyuan_id'];
        $colArr1 = ['hulipinggu_id', 'yiyuan_id'];
        $valArr = [$hulipinggu_id, $yiyuan_id];
        if ($pinggu_data) {
            $table = 'zhuyuan_huli_pinggu';
            CommonModel::delelteRows($table, $colArr1, $valArr);
        }
        if ($zhixing_data) {
            $table = 'zhuyuan_huli_pinggu_cuoshi_zhixing';
            CommonModel::delelteRows($table, $colArr1, $valArr);
        }
        $table = 'zhuyuan_huli_pinggu_hulidan';
        $affect = CommonModel::delelteRows($table, $colArr, $valArr);
        $result['state'] = $affect ? '1' : '0';
        $result['result'] = $affect ? '删除成功' : '删除失败';
        $result['msg'] = "";
        $result['error'] = 200;
        Utility::apiJsonResultExtends($result);
    }
}